package Dao_Klassen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Business_Klassen.DVD;
import Business_Klassen.Login;
import JFrames.JFrameKunde;

public class Login_Dao {

	private final static String CLASSNAME = "org.sqlite.JDBC";
	//private static String datei = "C:/Users/Ibo/Desktop/Schule/SQLiteSpy/DatenbankRPneu.db3";
	private static String datei = "C:/Users/Ibo/Desktop/Schule/SQLiteSpy/DatenbankRPneu.db3";
	private final static String CONNECTIONSTRING = "jdbc:sqlite:/" + datei;
	private Login login;
	private DVD dvd;
	static int benutzerid;
	public JFrameKunde dvd_id;
	

	public boolean pruefeKunde(char[] password, String userID)
			throws ClassNotFoundException {
		boolean gefunden = false;
		int user;
		Connection conn = null;
		Statement stmt;
		ResultSet rs;
		try {
			Class.forName(CLASSNAME);
				
			conn = DriverManager.getConnection(CONNECTIONSTRING);
			stmt = conn.createStatement();
			rs = stmt
					.executeQuery("select ID, Name, Passwort from LoginKunde");
			while (rs.next() && gefunden == false) {
				try {
					// for(int i=1;i<2;i++){
					if (rs.getString(2).equals(userID)
							&& rs.getString(3).equals(String.valueOf(password))) {
						gefunden = true;
						benutzerid= (rs.getInt("id"));
						

						

						// dvd.setId(resultSet.getInt("id"));
						// }else{
						// rs.next();
						// }

					}
					System.out.println(gefunden);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			stmt.close();
			rs.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return gefunden;
	}

	public boolean pruefeMitarbeiter(char[] password, String userID)
			throws ClassNotFoundException {
		boolean gefunden = false;
		Connection conn = null;
		Statement stmt;
		ResultSet rs;
		try {
			Class.forName(CLASSNAME);
			conn = DriverManager.getConnection(CONNECTIONSTRING);
			stmt = conn.createStatement();
			rs = stmt
					.executeQuery("select Name, passwort from LoginKunde");
			while (rs.next() && gefunden == false) {
				try {
					// for(int i=1;i<2;i++){
					if (rs.getString(1).equals(userID)
							&& rs.getString(2).equals(String.valueOf(password))) {
						gefunden = true;

					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			stmt.close();
			rs.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return gefunden;

	}

	public int registrieren(String Name, char[] Passwort) {
		Connection conn = null;
		String passwort = String.valueOf(Passwort);
		try {
			Class.forName(CLASSNAME);
			//conn = DriverManager
				//	.getConnection("jdbc:sqlite:/C:/Users/Ibo/Desktop/SQLiteSpy/DatenbankRP - Projekt.db3");
			conn = DriverManager.getConnection(CONNECTIONSTRING);
			Statement stmt = conn.createStatement();
			return stmt
					.executeUpdate("insert into LoginKunde (Name, Passwort) VALUES('"
							+ Name + "','" + passwort + "')");

		} catch (SQLException e) {
			return 0;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return 0;

	}
	
}
